<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class GoodsDepict extends Model
{
    use HasFactory;

    protected $table = 'sp_goods_depict_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $appends = [
        'imgurl_full'
    ];

    function getImgurlFullAttribute()
    {
        return '';
    }
    public function getCarouselImagesAttribute()
    {
        if (empty($this->carousel_images)) {
            return '';
        }
        $carousel_images = explode(',',$this->carousel_images);
        $imgs=[];
        foreach ($carousel_images as $imgurl){
           $imgs[]=config('alipay.oss.imgHost') . $this->imgurl;
        }
        return $imgs;
    }
    protected function AddGoodsDepict($goods_id,$carousel_img,$goods_content){
        $depict_data = [];
        $depict_data['goods_id'] = $goods_id;
        $depict_data['images'] = !empty($carousel_img)?$carousel_img:'';
        $depict_data['goods_content'] = !empty($goods_content)?$goods_content:'';
        $this->insert($depict_data);
    }

    protected function EditGoodsDepict($goods_id,$carousel_img,$goods_content){
        if($this->where('goods_id',$goods_id)->count() > 0 ){
            $depict_data = [];
            $depict_data['images'] = !empty($carousel_img)?$carousel_img:'';
            $depict_data['goods_content'] = !empty($goods_content)?$goods_content:'';
            $this->where('goods_id',$goods_id)->update($depict_data);
        }else{
            $depict_data = [];
            $depict_data['goods_id'] = $goods_id;
            $depict_data['images'] = !empty($carousel_img)?$carousel_img:'';
            $depict_data['goods_content'] = !empty($goods_content)?$goods_content:'';
            $this->insert($depict_data);
        }

    }
}
